package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.XiangXiXinWenDaoImpl;
import com.ysd.entity.XiangXiXinWen;
import com.ysd.util.BaseServlet;
import com.ysd.util.DBManager;
import com.ysd.util.Result;


public class AdminXinWenXiangQingServlet extends BaseServlet {
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=selXinWenXiangQing
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selXinWenXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
		ArrayList<XiangXiXinWen> xxxwList=xxxwDao.selXinWenXiangQing();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		Gson gson=new Gson();
		out.print(gson.toJson(xxxwList));
		
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=delXinWenXiangQingId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delXinWenXiangQingId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
		int xiangXiXinWenId=Integer.parseInt(request.getParameter("xiangXiXinWenId"));
		int n=xxxwDao.delXinWenXiangQingId(xiangXiXinWenId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(true, "删除失败", "您的操作有误"));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=selXinWenXiangQingWhere
	 * 分页多条件
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selXinWenXiangQingWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		int xinWenId=0;
		if(request.getParameter("xinWenId")!=null && !request.getParameter("xinWenId").equals("")) {
			xinWenId=Integer.parseInt(request.getParameter("xinWenId"));
		}
		int xiangXiXinWenId=0;
		if(request.getParameter("xiangXiXinWenId")!=null&& !request.getParameter("xiangXiXinWenId").equals("")) {
			xiangXiXinWenId=Integer.parseInt(request.getParameter("xiangXiXinWenId"));
		}
	
		String xinWenName=request.getParameter("xinWenName");
		String xinWenNeiRong=request.getParameter("xinWenNeiRong");
		String xinWenNameFenLei=request.getParameter("xinWenNameFenLei");
		
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		XiangXiXinWen xxxw=new XiangXiXinWen(xiangXiXinWenId, xinWenName, xinWenNeiRong, xinWenId, xinWenNameFenLei);
		XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
		ArrayList<XiangXiXinWen> xxxwlist=xxxwDao.selXinWenXiangQingWhere(xxxw,page,size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		Gson gson=new Gson();
		out.print(gson.toJson(xxxwlist));
		
		out.flush();
		out.close();
	}
	
	
/**
 * 	localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=selXinWenXiangQingWhereSize
 * 多条件查询新闻长度
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void selXinWenXiangQingWhereSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		int xinWenId=0;
		if(request.getParameter("xinWenId")!=null && !request.getParameter("xinWenId").equals("")) {
			xinWenId=Integer.parseInt(request.getParameter("xinWenId"));
		}
		int xiangXiXinWenId=0;
		if(request.getParameter("xiangXiXinWenId")!=null&& !request.getParameter("xiangXiXinWenId").equals("")) {
			xiangXiXinWenId=Integer.parseInt(request.getParameter("xiangXiXinWenId"));
		}
	
		String xinWenName=request.getParameter("xinWenName");
		String xinWenNeiRong=request.getParameter("xinWenNeiRong");
		String xinWenNameFenLei=request.getParameter("xinWenNameFenLei");
		XiangXiXinWen xxxw=new XiangXiXinWen(xiangXiXinWenId, xinWenName, xinWenNeiRong, xinWenId, xinWenNameFenLei);
		XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
		ArrayList<XiangXiXinWen> xxxwlist=xxxwDao.selXinWenXiangQingWhereSize(xxxw);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(xxxwlist.size()));
		
		out.flush();
		out.close();
	}
/**
 * 	localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=addXinWenXiangQing
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void addXinWenXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
		String xinWenName=request.getParameter("xinWenName");
		
		
		String xinWenNeiRong=request.getParameter("xinWenNeiRong");
		int xinWenId=Integer.parseInt(request.getParameter("xinWenId"));
		
		
		ArrayList<XiangXiXinWen> xxxwList=xxxwDao.selXiangXiXinWenByName(xinWenName);
		if(xxxwList.size()==0) {

			XiangXiXinWen xxxw=new XiangXiXinWen(0, xinWenName, xinWenNeiRong, xinWenId);
			
			int n=xxxwDao.addXinWenXiangQing(xxxw);
			
			if(n>0) {
				out.print(Result.toClient(true, "添加成功", ""));
			}else {
				out.print(Result.toClient(false, "添加失败", "您的操作有误"));
			}
			
		}else {
			out.print(Result.toClient(false, "您的标题重复", "您的操作有误"));
		}
		out.flush();
		out.close();
	}
	
/**
 * localhost:8080/xiyangyangchaoshi/adminxinwenxiangqing?reqName=selXiangXiXinWenById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void selXiangXiXinWenById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
	int xwid=Integer.parseInt(request.getParameter("xwid"));
	XiangXiXinWen xxxw=xxxwDao.selXiangXiXinWenById(xwid);
	
	response.setCharacterEncoding("UTF-8");
	PrintWriter out=response.getWriter();
	Gson gson=new Gson();
	out.print(gson.toJson(xxxw));
	
	
	out.flush();
	out.close();
}


/**
 * adminxinwenxiangqing?reqName=editXinWenXiangQingById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void editXinWenXiangQingById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("UTF-8");
	PrintWriter out=response.getWriter();
	XiangXiXinWenDaoImpl xxxwDao=new XiangXiXinWenDaoImpl();
	
	
	String xinWenName=request.getParameter("xinWenName");
	String xinWenNeiRong=request.getParameter("xinWenNeiRong");
	int xinWenId=Integer.parseInt(request.getParameter("xinWenId"));
	int xiangXiXinWenId=Integer.parseInt(request.getParameter("xiangXiXinWenId"));
	XiangXiXinWen xiangXiXinWen=new XiangXiXinWen(xiangXiXinWenId, xinWenName, xinWenNeiRong, xinWenId);
	
	
	
	ArrayList<XiangXiXinWen> xxxwList=xxxwDao.selXiangXiXinWenByName(xinWenName);
	int n=xxxwDao.editXiangXiXinWenById(xiangXiXinWen);
	if(xxxwList.size()==0) {
		if(n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		}else {
			out.print(Result.toClient(false, "修改失败", "您的操作有误"));
		}
	}else {
		out.print(Result.toClient(false, "您的标题已存在", "您的操作有误"));
	}
	out.flush();
	out.close();
}

	
}
